class Solution
{
public:
    string shortestBeautifulSubstring(string s, int k)
    {
        int n = s.size();
        vector<int> prefixCount(n + 1);
        for (int i = 0; i < n; ++i)
        {
            prefixCount[i + 1] = prefixCount[i] + (s[i] == '1');
        }

        string result = "";
        for (int start = 0; start < n; ++start)
        {
            for (int end = start; end < n; ++end)
            {
                int count = prefixCount[end + 1] - prefixCount[start];
                if (count == k)
                {
                    string sub = s.substr(start, end - start + 1);
                    if (result.empty())
                    {
                        result = sub;
                    }
                    else if (sub.size() < result.size())
                    {
                        result = sub;
                    }
                    else if (sub.size() == result.size() && sub < result)
                    {
                        result = sub;
                    }
                }
            }
        }
        return result;
    }
};